CC ?= gcc
SHARED := -fPIC --shared
SKYNET_PATH		?=3rd/skynet
BIN_PATH		?=bin
SKYNET_SRC_PATH ?= $(SKYNET_PATH)/skynet-src
LUA_CLIB_PATH	?= $(BIN_PATH)/luaclib
LUA_LIB_PATH	?= $(BIN_PATH)/lualib

CFLAGS = -g -O0 -Wall -I$(LUA_INC) $(MYCFLAGS)

LUA_INC ?= $(SKYNET_PATH)/3rd/lua

LUA_CLIB = cjson

all : \
  $(foreach v, $(LUA_CLIB), $(LUA_CLIB_PATH)/$(v).so) \
  LUA \
  SKYNET

$(LUA_CLIB_PATH) :
	mkdir -p $(LUA_CLIB_PATH)

$(LUA_LIB_PATH) :
	mkdir $(LUA_LIB_PATH)
LUA:$(LUA_LIB_PATH)

.PHONY:SKYNET
SKYNET:
	cd  $(SKYNET_PATH) && make linux
	cp $(SKYNET_PATH)/luaclib/* $(LUA_CLIB_PATH)
	cp -r $(SKYNET_PATH)/lualib/*  $(LUA_LIB_PATH) 
	cp -r $(SKYNET_PATH)/service  $(BIN_PATH) 
	cp -r $(SKYNET_PATH)/cservice  $(BIN_PATH) 
	cp  $(SKYNET_PATH)/skynet  $(BIN_PATH) 

$(LUA_CLIB_PATH)/cjson.so : 3rd/lua-cjson/fpconv.c  3rd/lua-cjson/lua_cjson.c 3rd/lua-cjson/strbuf.c | $(LUA_CLIB_PATH)
	$(CC) $(CFLAGS) $(SHARED) -I3rd/lua-cjson/ -I$(SKYNET_SRC_PATH) $^ -o $@

clean :
	rm -rf $(BIN_PATH)/*

echo:
	echo $(LUA_INC)

cleanall: clean

